Skip to content

[Feature] [TPU] deepseek-v4.1: Add prompt-tail decoder replay - #2417

Draft
cjx0709 wants to merge 5 commits into
mainfrom
develop/dsv41-ced-bounded
Draft

cjx0709 wants to merge 5 commits into
mainfrom
develop/dsv41-ced-bounded

Conversation

@cjx0709

@cjx0709 cjx0709 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Keep building the full global KV cache during long cached prefills and run the late decoder layers only for the final SWA window of each prompt. This reduces work on chunks that are still before the decoder window.

Modifications

  • Keep TPU decoder replay opt-in through --enable-decoder-swa-bounded-replay. The implementation requires encoder SWA replay, shifted mHC, the direct TPU compiler, and monolithic text serving. Routed expert capture is rejected because its full-layout output would not match compact decoder rows.
  • Write the last global KV source on all encoder rows, then skip the late layers when every attention-DP rank has zero decoder rows. Otherwise, run the decoder tail on a shared compile bucket with per-rank valid rows.
  • Restore the full extend layout for logits and captured hidden states. Limit draft SWA writes to the retained rows. Preserve full decoder history for requests that read prompt logprobs or full hidden states. Output-only logprobs use the same replay policy as generation.
  • Register TPU-side hooks at model initialization and forward, following [Fix] [TPU] deepseek-v4: Boot on TPU after the upstream sync (pool hook, mHC, Engram gate) #2367. The prefill hook is active only for a prepared TPU decoder tail. The PR diff leaves python/sglang/srt/models/ identical to main.

Accuracy Tests

True-weight DeepSeek-V4.1-Flash with DSpark K5 on one 4-physical-chip v7x host, DP8/EP8, direct tracer with SPMD, and TorchTPU 0.3.0. The device measurements below are from c1be650215, before the output-only logprob fix and the plugin hook migration.

  • Five paired waves of eight long code prompts completed. Every request hit 98,304 cached tokens and extended by 16,384 tokens. For each prompt, the first generated token matched across all waves and both arms.
  • A separate cold 4,096-token code prompt produced 32 greedy tokens in each arm. The first generated token already differed, before the first K5 verify. With decoder replay off, 9 of 120 proposed draft tokens were accepted (7.50%), and the mean acceptance length was 1.33. With replay on, 1 of 150 was accepted (0.67%), and the mean acceptance length was 1.07. The mean includes the target token. The single-request result leaves both the scope and the cause of the difference open.
  • Local checks covered host row planning, metadata and pytree behavior, and two-rank CPU Gloo cases for mixed and all-zero decoder rows. The hook tests use CPU tensors and stubbed runtime dependencies to verify the full source write, decoder skip, DSpark output layout, and fallback on target verify.

Before marking ready for review, compare first-token logits on the cold 4K case against a same-weight reference using the same prompt-tail policy. Check the source-layer global KV, tail hidden states, and decoder SWA before comparing the first verify and post-commit draft KV. Repeat across cold and cached inputs with a representative DSpark workload. Rerun output-only logprobs on a TPU server without speculation. Collect first-token logits internally for the K5 run because the experimental TPU spec worker does not serve logprobs.

Speed Tests and Profiling

Five warm measurement waves per arm used the same machine, checkpoint, fixture, and runtime. The step metric is host cadence between adjacent prefill markers when all eight ranks processed 2,048 new rows at both endpoints. The measured intervals exclude admission, boundary steps, and logged recompiles.

Decoder replay Matched middle intervals p50 p95
Off 20 476.5 ms 480.1 ms
On 19 222.0 ms 225.1 ms

The replay arm reduced median middle-step cadence by 53.4%. Runtime logs showed zero decoder rows across the eight ranks on those interior steps and 128 live decoder rows per rank at the final prompt step. The single-step XPlane begins the encoder computation and ends before the full device program completes. A complete trace remains pending.


CI States

Latest PR Test (Base): ❌ Run #45168486659
Latest PR Test (Extra): ❌ Blocked -- run-ci is required first.
Latest PR Test (AMD ROCm 10): ➖ No AMD PR run found for this commit.

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@github-actions

Copy link
Copy Markdown
Contributor

A user listed in .github/CI_PERMISSIONS.json must comment /tag-and-rerun-ci to trigger the required TPU checks. Repeat this after every new push, including rebases.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant